#include "common.h"
#include "drv_fpga.h"
#define     LOG_DEBUG_AB_INIT   0
#if LOG_DEBUG_AB_INIT
#define     LOG_DEBUG_AB(format, ...)
#else
#define     LOG_DEBUG_AB(format, ...)     LOG_DEBUG(format, ##__VA_ARGS__);  
#endif
int fix_freq[ABP_FREQ_NUM] = { ABP_FREQ_9330MHZ, ABP_FREQ_9370MHZ, ABP_FREQ_9410MHZ, ABP_FREQ_9450MHZ, ABP_FREQ_9490MHZ };
uint8 net_buf[VALN_BUF_LEN] = { 0 };
uint8 *calibErrordata = NULL;                       //校准误差的数据 
double ptr[AB_RANDE_NUM][AB_CHANNEL_NUM];
AB_PData *AbpData = NULL;                           //save Amplitude-phase chart data
FanShapedSector fShapedSector[SECTOR_NUM] = { 0 };  //Record the sector scan range

uint8 ParaModule = Para_NETWORK;
uint8 ParaVal = 0x0;
uint8 IS_CALIB = 0x01;                              //校准数据,默认是加的, 0x01
uint8 IS_COEFF = 0x01;                              //加权系数,默认是加的, 0x01
extern int scan_type;                               //扫描类型
MBOOL is_FanShaped(double cuml_deg) {
    int cur_deg = (int)(cuml_deg*DEG_UINT);         //范围 0~36000°
    int cnt = 0;
    MBOOL res = MFAULSE;
    FanShapedSector fshstor;
    for(;cnt < SECTOR_NUM; cnt++) {
        fshstor = fShapedSector[cnt];
        if(fshstor.valid == FS_SECTOR_VALID_NO) {
            continue;
        }
        if(fshstor.start_deg < fshstor.end_deg) {
            if(cur_deg >= fshstor.start_deg && cur_deg <= fshstor.end_deg) {
                res = MTRUE;
                goto GO_BACK;
            }
        }

        if(fshstor.start_deg > fshstor.end_deg) {
            if((cur_deg <= (DEG_MAX_360*100) && cur_deg >= fshstor.start_deg) || (cur_deg >=0 && cur_deg <= fshstor.end_deg)) {
                res = MTRUE;
                goto GO_BACK;
            }
        }
    }

GO_BACK:
    return res;
}
uint8 amplitBuff[AMPLITBUFF_LEN];
void printAmpBuff() {
    int i = 0; 
    int channel = 0;
    for(; i < AMPLITBUFF_LEN; i++) {
        if(i%16 == 0) {
            channel++;
            printf("\n spi: %02d, ", channel);
        }
        printf("%02x ", amplitBuff[i]);
    }
    printf("\n");
    exit(0);
}

int getNearFreq(int freq) {
    int cnt = 0;
    int minval = 0;
    int cminval = 0;
    int index = 0;

    for(; cnt < ABP_FREQ_NUM; cnt++) {;
        cminval = abs(fix_freq[cnt] - freq);
        if(cnt == 0) {
            minval = cminval;
            continue;
        }
        if(cminval <= minval) {
            minval = cminval;
            index = cnt;
        }
    }
    return index;
}

// 获取幅度衰减, 衰减值,正数值
uint8 getrange(double deg) {
    double temp = 0;
    int cdeg = 0;
    uint8 ch = 0;

    deg = (int)(deg*DEG_FOR_COMPAR) \
        - RANG_MAX_31_49 * DEG_FOR_COMPAR >= 0 \
        ? RANG_MAX_31_49 : deg;    //临界值处理
    
    temp = deg*2 + 0.5;    
    cdeg = (int) temp;
    ch = (uint8) cdeg;
    return ch;
}

// 获取相位, 相位角度值
uint8 getphase(double deg) {
    double temp = 0;
    int cdeg = 0;
    uint8 ch = 0;

    deg = ((int)(deg*DEG_FOR_COMPAR)  \
        - (int)(DEG_MAX_360*DEG_FOR_COMPAR)) >= 0 \
        ?  deg - DEG_MAX_360 : deg;  //对360°临界值处理

    deg = ((int)(deg*DEG_FOR_COMPAR)  \
        - (int)(PHSE_MAX_357_X*DEG_FOR_COMPAR)) >= 0 \
        ?  0 : deg;                  //对357.1875°临界值处理

    temp = (deg / DEG_UINT_TO_U8BIN) + 0.5;
    cdeg = (int) temp;
    ch = (uint8) cdeg;
    ch |= U8_BIT(7);        // enabled
    return ch;
}
// 计算相位, deg * 10, 计算精度提高1,转换成int, 提高运算效率
uint8 calc_phase_rang(double *pdata, uint16 *calib, uint8 start_channel, uint8 cur_channel, uint8 type, uint8 module) {
    uint8 ch = 0;
    int rem = 0;
    int temp = 0;
    double deg = 0;
    int degindex = 0;
    // 判断模式
    if(ParaModule == Para_RX_COMP || ParaModule == Para_TX_COMP) {
        if(type != DTYPE_PHASE) {
            return 0x0;
        }
        if(ParaModule != module) {
            return 0x0;
        }
        if(start_channel != (cur_channel + 1)) {
            return 0x0;
        }
        // ch = (uint8)0x80;
        ch = (uint8) ParaVal;
        LOG_INFO("=> channel: %d, ichannel: %d, ch: %02x", start_channel, cur_channel, ch);
        return ch;
    }

    // 起始通道数是否大于64, 考虑环形
    uint8 flag = 0;
    cur_channel = cur_channel + (uint8)0x01;

    if(start_channel > 65) {  //65 时, 范围为 65~128, 
        if(!((cur_channel >= start_channel && cur_channel <= 128) || (cur_channel > 0 && cur_channel <= (start_channel - 65)))) {
            return 0;
        }
    } else {
        if(!(cur_channel >= start_channel && cur_channel <= (start_channel + 63))) {
            return 0;
        }
    }

    if(cur_channel >= start_channel) {
        degindex = cur_channel - start_channel + 1;
    } else {
        degindex = (128 - start_channel) + cur_channel + 1;
    }

    if(!(degindex >= 1 && degindex <= 64)) {
        LOG_ERROR("The calculated amplitude Angle subscript is wrong, degindex: %d, cur_channel %d, start_channel %d ", degindex, cur_channel, start_channel);
        return 0;
    }
    
    // 计算误差校准
    double temp_deg = 0.0;
    if(NULL != calib && IS_CALIB != 0x00) {
        temp_deg = calib[cur_channel - 1] * DEG_UINT_NET;
#if MODULE_TEST
        if(((int)temp_deg) != 0.0) {
            LOG_DEBUG("===> %.2f, channel: %d", temp_deg, cur_channel - 1);
        }
#endif
    }

    if(type == DTYPE__RANG){
        deg = IS_COEFF || IS_CALIB ? pdata[degindex - 1] + temp_deg : 0;
        ch = getrange(deg);
    } else if(type == DTYPE_PHASE) {
        deg = IS_COEFF ? pdata[degindex] + temp_deg : temp_deg;
        // LOG_DEBUG("%.3lf \t%.3lf \t%.3lf \t%04x", pdata[degindex], temp_deg, deg, calib[cur_channel - 1]);
        ch = getphase(deg);
    } else {
        LOG_ERROR("The current type is not supported, type: %02x ", type);
        return 0;
    }
    // LOG_DEBUG_AB("%s: start_channel: %d, cur_channel: %d, ch: %02x, degindex: %02d, deg: %.2lf",type == DTYPE__RANG ? "__RANG":"_PHASE", start_channel, cur_channel, ch, degindex + 1, deg);
    return ch;
}

int pack_amplit(int tindex, int rindex, Para_Model *pmodel) {
    // LOG_DEBUG_AB("sizeof amplitBuff: %d ", sizeof(amplitBuff));
    int channel = pmodel->start_channel;
    int offset_index =  0;
    int offset_channel = 0;
    double *tpdata = AbpData[tindex].rbuf[pmodel->deg_index];
    double *rpdata = AbpData[rindex].rbuf[pmodel->deg_index];
    
    uint16 *tdamp  = (uint16 *)pmodel->calib_tx_damp;
    uint16 *tphase = (uint16 *)pmodel->calib_tx_phase;
    uint16 *rdamp  = (uint16 *)pmodel->calib_rx_damp;
    uint16 *rphase = (uint16 *)pmodel->calib_rx_phase;

    send_angle(pmodel->cur_deg);        //下发角度 [光口]

    int i = 0;
    memset(amplitBuff, 0x0, sizeof(amplitBuff));

    // 封装Tx数据
    // LOG_INFO("Package Tx data");
    // for(i = 0; i < 16; i++) {
    //     printf("%.3lf ", T_AMPLIT[i]);
    // }
    // printf("\n");



    for(i = 0; i < 16; i++) {
        offset_index = i*16;
        offset_channel = i*8;
        amplitBuff[offset_index +  0] = calc_phase_rang(tpdata, tphase, channel, i*8 + 0, DTYPE_PHASE, Para_TX_COMP);
        amplitBuff[offset_index +  1] = calc_phase_rang(tpdata, tphase, channel, i*8 + 1, DTYPE_PHASE, Para_TX_COMP);
        amplitBuff[offset_index +  2] = calc_phase_rang(tpdata, tphase, channel, i*8 + 2, DTYPE_PHASE, Para_TX_COMP);
        amplitBuff[offset_index +  3] = calc_phase_rang(tpdata, tphase, channel, i*8 + 3, DTYPE_PHASE, Para_TX_COMP);
        amplitBuff[offset_index +  4] = calc_phase_rang(tpdata, tphase, channel, i*8 + 4, DTYPE_PHASE, Para_TX_COMP);
        amplitBuff[offset_index +  5] = calc_phase_rang(tpdata, tphase, channel, i*8 + 5, DTYPE_PHASE, Para_TX_COMP);
        amplitBuff[offset_index +  6] = calc_phase_rang(tpdata, tphase, channel, i*8 + 6, DTYPE_PHASE, Para_TX_COMP);
        amplitBuff[offset_index +  7] = calc_phase_rang(tpdata, tphase, channel, i*8 + 7, DTYPE_PHASE, Para_TX_COMP);

        amplitBuff[offset_index +  8] = calc_phase_rang(T_AMPLIT, tdamp, channel, i*8 + 0, DTYPE__RANG, Para_TX_COMP);
        amplitBuff[offset_index +  9] = calc_phase_rang(T_AMPLIT, tdamp, channel, i*8 + 1, DTYPE__RANG, Para_TX_COMP);
        amplitBuff[offset_index + 10] = calc_phase_rang(T_AMPLIT, tdamp, channel, i*8 + 2, DTYPE__RANG, Para_TX_COMP);
        amplitBuff[offset_index + 11] = calc_phase_rang(T_AMPLIT, tdamp, channel, i*8 + 3, DTYPE__RANG, Para_TX_COMP);
        amplitBuff[offset_index + 12] = calc_phase_rang(T_AMPLIT, tdamp, channel, i*8 + 4, DTYPE__RANG, Para_TX_COMP);
        amplitBuff[offset_index + 13] = calc_phase_rang(T_AMPLIT, tdamp, channel, i*8 + 5, DTYPE__RANG, Para_TX_COMP);
        amplitBuff[offset_index + 14] = calc_phase_rang(T_AMPLIT, tdamp, channel, i*8 + 6, DTYPE__RANG, Para_TX_COMP);
        amplitBuff[offset_index + 15] = calc_phase_rang(T_AMPLIT, tdamp, channel, i*8 + 7, DTYPE__RANG, Para_TX_COMP);
    }

    // 封装Rx数据
    // LOG_INFO("Package Rx data");
    for(i = 0; i < 16; i++) {
        offset_index = i*16 + 16*16;
        offset_channel = i*8;
        amplitBuff[offset_index +  0] = calc_phase_rang(rpdata, rphase, channel, i*8 + 0, DTYPE_PHASE, Para_RX_COMP);
        amplitBuff[offset_index +  1] = calc_phase_rang(rpdata, rphase, channel, i*8 + 1, DTYPE_PHASE, Para_RX_COMP);
        amplitBuff[offset_index +  2] = calc_phase_rang(rpdata, rphase, channel, i*8 + 2, DTYPE_PHASE, Para_RX_COMP);
        amplitBuff[offset_index +  3] = calc_phase_rang(rpdata, rphase, channel, i*8 + 3, DTYPE_PHASE, Para_RX_COMP);
        amplitBuff[offset_index +  4] = calc_phase_rang(rpdata, rphase, channel, i*8 + 4, DTYPE_PHASE, Para_RX_COMP);
        amplitBuff[offset_index +  5] = calc_phase_rang(rpdata, rphase, channel, i*8 + 5, DTYPE_PHASE, Para_RX_COMP);
        amplitBuff[offset_index +  6] = calc_phase_rang(rpdata, rphase, channel, i*8 + 6, DTYPE_PHASE, Para_RX_COMP);
        amplitBuff[offset_index +  7] = calc_phase_rang(rpdata, rphase, channel, i*8 + 7, DTYPE_PHASE, Para_RX_COMP);

        amplitBuff[offset_index +  8] = calc_phase_rang(R_AMPLIT, rdamp, channel, i*8 + 0, DTYPE__RANG, Para_RX_COMP);
        amplitBuff[offset_index +  9] = calc_phase_rang(R_AMPLIT, rdamp, channel, i*8 + 1, DTYPE__RANG, Para_RX_COMP);
        amplitBuff[offset_index + 10] = calc_phase_rang(R_AMPLIT, rdamp, channel, i*8 + 2, DTYPE__RANG, Para_RX_COMP);
        amplitBuff[offset_index + 11] = calc_phase_rang(R_AMPLIT, rdamp, channel, i*8 + 3, DTYPE__RANG, Para_RX_COMP);
        amplitBuff[offset_index + 12] = calc_phase_rang(R_AMPLIT, rdamp, channel, i*8 + 4, DTYPE__RANG, Para_RX_COMP);
        amplitBuff[offset_index + 13] = calc_phase_rang(R_AMPLIT, rdamp, channel, i*8 + 5, DTYPE__RANG, Para_RX_COMP);
        amplitBuff[offset_index + 14] = calc_phase_rang(R_AMPLIT, rdamp, channel, i*8 + 6, DTYPE__RANG, Para_RX_COMP);
        amplitBuff[offset_index + 15] = calc_phase_rang(R_AMPLIT, rdamp, channel, i*8 + 7, DTYPE__RANG, Para_RX_COMP);
    }
    // printAmpBuff(); //打印测试数据
    send_TRX((uint32 *)amplitBuff, AMPLITBUFF_LEN/4);
    return KT_OK;
}

// Query the corresponding data in the amplitude and phase table
// int query_amplit(int deg_index, int scan_freq, int start_channel) {
int query_amplit(Para_Model *pmodel) {
    // LOG_DEBUG_AB("start find table deg_index %d with cur_scan_freq %dMHz ", deg_index, scan_freq);
    int t_index = 0;
    int r_index = 0;
    int cnt = AB_TABLE_NUM;
    for(cnt = 0; cnt < AB_TABLE_NUM; cnt++) {
        if(AbpData[cnt].freq == pmodel->scan_freq && AbpData[cnt].type == ABP_TYPE_SEND) {
            t_index = cnt;
        }
        
        if(AbpData[cnt].freq == pmodel->scan_freq && AbpData[cnt].type == ABP_TYPE_RECV) {
            r_index = cnt;
        }
    }
    // LOG_DEBUG_AB("scanfreq: %dMHz, abp T_index: %d, abp R_index: %d, deg_index: %d, "\
    "tfrist: %.6lf, tlast: %.6lf, rfrist: %.6lf, rlast: %.6lf", \
        scan_freq, t_index, r_index, deg_index, \
        AbpData[t_index].rbuf[deg_index][1], AbpData[t_index].rbuf[deg_index][AB_CHANNEL_NUM], \
        AbpData[r_index].rbuf[deg_index][1], AbpData[r_index].rbuf[deg_index][AB_CHANNEL_NUM]);
    pack_amplit(t_index, r_index, pmodel);
    return KT_OK;
}
int openOne_TRXModule(int channel) {
    IS_CALIB = 0;  
    scan_type = Test_Verify;
    Para_Model pmodel;       //不加误差校验
    memset(&pmodel, 0x0, sizeof(Para_Model));
    pmodel.start_channel = channel;
    pack_amplit(0, 0, &pmodel);
}
int InitAllZero() {
    // 发送全零
    int cnt = 0;
    for(cnt = 0; cnt < AMPLITBUFF_LEN; cnt++) {
        amplitBuff[cnt] = 0x00;
    }
#if !MODULE_TEST
    send_TRX((uint32 *)amplitBuff, AMPLITBUFF_LEN/4);
#else 
    usleep(1000*10);
#endif // MODULE_TEST
    return KT_OK;
}
// Gets the nearest position of the relative amplitude-phase table
// int get_index_head(double diff_deg, int channel, int cindex, double cuml_deg) {
void get_index_head(Para_Model *pmodel) {
    int curdeg = (int)(pmodel->diff_deg * DEG_UINT);
    int cnt;
    int minval = 0;
    int cminval = 0;
    int index = 0;
    for(cnt = 0; cnt < AB_RANDE_NUM; cnt++) {
        minval = abs(curdeg - check_head[cnt]);
        if(cnt == 0) {
            cminval = minval;
        } else {
            if(cminval > minval) {
                cminval = minval;
                index = cnt;
            }
        }
        // LOG_DEBUG_AB("curdeg: %d, minval: %d, cminval: %d", curdeg, minval, cminval);
    }
    if(scan_type == Test_Verify) {
        LOG_INFO("cuml_deg: %.3lf, channel: %d, cnt: %d, diffdeg: %.6lf, get Angle: %.2lf, deg_index: %d",\
                pmodel->cur_deg, pmodel->start_channel, pmodel->index, pmodel->diff_deg, (double)(check_head[index]/100.0), index);
    }else {
        LOG_DEBUG("cuml_deg: %.3lf, channel: %d, cnt: %d, diffdeg: %.6lf, get Angle: %.2lf, deg_index: %d",\
                pmodel->cur_deg, pmodel->start_channel, pmodel->index, pmodel->diff_deg, (double)(check_head[index]/100.0), index);
    }
    // LOG_DEBUG("diff_deg: %.6lf, start_channel: %d", diff_deg, start_channel);
    pmodel->deg_index = index;
}

int check_pack_head(double ptr[AB_RANDE_NUM][AB_CHANNEL_NUM + 1]) {
    int i = 0;
    int res = 0;
    int temp = 0;
    for(; i < AB_RANDE_NUM; i++) {
        temp = (int)(ptr[i][0]);
        int err_rang = temp - check_head[i];
        if(err_rang != 0) {
            res = -1;
            LOG_ERROR("Error initializing amplitude table, %d %d ", temp, check_head[i]);
        }
    }
    return res;
}
static int global_findex = 0;                               //记录当前文件下载的序列号, 从 1 开始
void send_net_data(uint8 *cbuf, int clen);
void read_to_flash(uint8 *fbuf, int flen, char *fname);
void write_to_flash(uint8 *fbuf, int flen, char *fname);
void sendFileDownStatus(DFILE_Status status) {
    uint8 responseData[5] = {0xBB, (uint8)File_Download, (uint8)status, ~(uint8)File_Download, 0xEE};
    send_net_data(responseData, sizeof(responseData));
} 
int file_Download(uint8 * rbuf) {
    DloadFile dfile;
    memcpy(&dfile, rbuf, sizeof(DloadFile));
    uint8 *bptr = calibErrordata;
    if(NULL == bptr) {
        LOG_ERROR("#ERR: bptr is NULL");
        return KT_ERROR;
    }

    if(dfile.filesize != CalibErrorDLEN) {
        sendFileDownStatus(D_STATUS_FAILED);
        LOG_ERROR("Calibration data buff size: %d, but filesize: %d", CalibErrorDLEN, dfile.filesize);
        return KT_ERROR;
    }
    
    if(global_findex != dfile.fileindex - 1) {          //发生了包丢失
        sendFileDownStatus(D_STATUS_FAILED);
        LOG_ERROR("packet loss occurred ");
        global_findex = 0;
        return KT_ERROR;
    }

    global_findex = dfile.fileindex;

    if(global_findex == 1) {
        LOG_INFO("module is file download ....");       //需要初始化一些参数
        memset(bptr, 0x0, CalibErrorDLEN);
    }

    memcpy(bptr + (global_findex - 1)*1400, dfile.fbuf,  dfile.filelen);
    int fsize = (global_findex - 1)*1400 + dfile.filelen;
    
    if(fsize == dfile.filesize) {
        sendFileDownStatus(D_STATUS_OVER);
        LOG_INFO("file download successful ");
        if(dfile.updateType == 0x01) {
            write_to_flash(bptr, CalibErrorDLEN, CalibErrorDNAME);
        }
        global_findex = 0;
        // printHex1(bptr, CalibErrorDLEN, 128*2, "update-calilb");
        LOG_DEBUG("receiving packed index: %d, cflen: %d, fsize: %d", global_findex, fsize, dfile.filesize);
        return KT_OK;
    }
    sendFileDownStatus(D_STATUS_SUCCSE);
    LOG_DEBUG("receiving packed index: %d, cflen: %d, fsize: %d", global_findex, fsize, dfile.filesize);
}

int check_AB_data() {
    if(check_pack_head(T_9330) == -1) {
        LOG_ERROR("T_9330 table data has error");
    }
    if(check_pack_head(T_9370) == -1) {
        LOG_ERROR("T_9370 table data has error");
    }
    if(check_pack_head(T_9410) == -1) {
        LOG_ERROR("T_9410 table data has error");
    }
    if(check_pack_head(T_9450) == -1) {
        LOG_ERROR("T_9450 table data has error");
    }
    if(check_pack_head(T_9490) == -1) {
        LOG_ERROR("T_9490 table data has error");
    }
    if(check_pack_head(R_9330) == -1) {
        LOG_ERROR("R_9330 table data has error");
    }
    if(check_pack_head(R_9370) == -1) {
        LOG_ERROR("R_9370 table data has error");
    }
    if(check_pack_head(R_9410) == -1) {
        LOG_ERROR("R_9410 table data has error");
    }
    if(check_pack_head(R_9450) == -1) {
        LOG_ERROR("R_9450 table data has error");
    }
    if(check_pack_head(R_9490) == -1) {
        LOG_ERROR("R_9490 table data has error");
    }
}
void AB_free() {
    free(AbpData);
    free(calibErrordata);
    LOG_INFO("AB resources free sucessful!");
}
// Examine the data in the amplitude and phase table and initialize it as an array
void AB_init() {
    check_AB_data();                        //Examine the data in the amplitude and phase table

    AbpData = (AB_PData *)malloc(AB_TABLE_NUM * sizeof(AB_PData));
    if(NULL == AbpData) {
        LOG_ERROR("#ERR to malloc AbpData");
        return;
    }
    memset(AbpData, 0x0, sizeof(AbpData));

    AbpData[0].type = ABP_TYPE_SEND;
    AbpData[0].freq = ABP_FREQ_9330MHZ;
    memcpy(&AbpData[0].rbuf, T_9330, sizeof(T_9330));

    AbpData[1].type = ABP_TYPE_SEND;
    AbpData[1].freq = ABP_FREQ_9370MHZ;
    memcpy(&AbpData[1].rbuf, T_9370, sizeof(T_9370));

    AbpData[2].type = ABP_TYPE_SEND;
    AbpData[2].freq = ABP_FREQ_9410MHZ;
    memcpy(&AbpData[2].rbuf, T_9410, sizeof(T_9410));

    AbpData[3].type = ABP_TYPE_SEND;
    AbpData[3].freq = ABP_FREQ_9450MHZ;
    memcpy(&AbpData[3].rbuf, T_9450, sizeof(T_9450));

    AbpData[4].type = ABP_TYPE_SEND;
    AbpData[4].freq = ABP_FREQ_9490MHZ;
    memcpy(&AbpData[4].rbuf, T_9490, sizeof(T_9490));

    AbpData[5].type = ABP_TYPE_RECV;
    AbpData[5].freq = ABP_FREQ_9330MHZ;
    memcpy(&AbpData[5].rbuf, R_9330, sizeof(R_9330));

    AbpData[6].type = ABP_TYPE_RECV;
    AbpData[6].freq = ABP_FREQ_9370MHZ;
    memcpy(&AbpData[6].rbuf, R_9370, sizeof(R_9370));

    AbpData[7].type = ABP_TYPE_RECV;
    AbpData[7].freq = ABP_FREQ_9410MHZ;
    memcpy(&AbpData[7].rbuf, R_9410, sizeof(R_9410));

    AbpData[8].type = ABP_TYPE_RECV;
    AbpData[8].freq = ABP_FREQ_9450MHZ;
    memcpy(&AbpData[8].rbuf, R_9450, sizeof(R_9450));

    AbpData[9].type = ABP_TYPE_RECV;
    AbpData[9].freq = ABP_FREQ_9490MHZ;
    memcpy(&AbpData[9].rbuf, R_9490, sizeof(R_9490));
    
    LOG_INFO("AB_para data init successful");

    calibErrordata = (uint8 *)malloc(CalibErrorDLEN);

    if(NULL == calibErrordata) {
        LOG_ERROR("#ERR: cailb_error_data malloc failed");
        LOG_ERROR("%d : %s",errno,strerror(errno));
        exit(-1);
    }
    LOG_INFO("malloc calibErrordata addr: %p", calibErrordata);
    read_to_flash(calibErrordata, CalibErrorDLEN, CalibErrorDNAME);

#if MODULE_TEST
    int base_addr = 128;
    uint16 *ptr = (uint16 *)calibErrordata;
    // calibErrordata[base_addr*0] = 100;
    LOG_DEBUG("calib base addr: 0x%0x", ptr);
    // ptr[base_addr*0+16] = 0x258;
    // ptr[base_addr*0+17] = 0x258;
    ptr[base_addr*1] = 0;
    ptr[base_addr*2] = 0;
    // ptr[base_addr*3 + 17] = 0x256;
    // ptr[base_addr*4 + 16] = 0x456;
    // ptr[base_addr*4 + 17] = 0x256;
    ptr[base_addr*5] = 0;
    ptr[base_addr*6] = 0;
    ptr[base_addr*7] = 0;
    ptr[base_addr*8] = 0;
    ptr[base_addr*10] = 0;
#endif 

}
